package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.ui.activity.ResourceDetailActivity;
import com.example.dangerouscargodriver.ui.activity.UserWalletActivity;

public class ResourceInfoView extends LinearLayout implements View.OnClickListener {

    private static final String TAG = "ResourceInfoView";
    private TextView mFromName = null;
    private TextView mToName = null;
    private TextView mSourceDistance = null;
    private TextView mRegTime = null;
    private Context mContext = null;
    private LinearLayout mMetaLayout = null;
    private Button mActionGrabOrder = null;
    private ResourceInfo mResourceInfo = null;
    private LinearLayout mLayout = null;
    private ImageView mLevelView = null;
    private TextView mLoadTime = null;
    private TextView mExpectedPrice = null;
    private LinearLayout mPayOnline = null;
    private LinearLayout mShowPrice = null;

    public ResourceInfoView(Context context, ResourceInfo info) {
        this(context, null, info);
    }

    public ResourceInfoView(Context context, AttributeSet attrs, ResourceInfo info) {
        this(context, attrs, 0, info);
    }

    public ResourceInfoView(Context context, AttributeSet attrs, int defStyleAttr, ResourceInfo info) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, info);
    }

    public void init(Context context, AttributeSet attrs, ResourceInfo info) {
        mContext = context;
        mResourceInfo = info;
        View itemLayout = LayoutInflater.from(context).inflate(R.layout.layout_resource_info_item, this);
        mFromName = itemLayout.findViewById(R.id.item_start_city);
        mSourceDistance = itemLayout.findViewById(R.id.item_source_distance);
        mToName = itemLayout.findViewById(R.id.item_end_city);
        mRegTime = itemLayout.findViewById(R.id.item_register_time);
        mActionGrabOrder = itemLayout.findViewById(R.id.btn_grab_order);
        mActionGrabOrder.setOnClickListener(this);
        mMetaLayout = itemLayout.findViewById(R.id.layout_resource_meta);
        mLevelView = findViewById(R.id.iv_dangerous_level);
        mLoadTime = findViewById(R.id.tv_load_time);
        mShowPrice = findViewById(R.id.layout_show_price);
        mPayOnline = findViewById(R.id.layout_pay_online);
        mExpectedPrice = findViewById(R.id.tv_expected_price);

        //执行绑定数据的操作
        mFromName.setText(info.getFrom_name());
        mToName.setText(info.getTo_name());
        mSourceDistance.setText(info.getSource_distance());
        mRegTime.setText(info.getRegtime());
        if(info.getName() != null && !info.getName().equals("")){
            View layout = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
            TextView textView = layout.findViewById(R.id.tv_item_meta_string);
            textView.setText(info.getName());
            mMetaLayout.addView(layout);
        }
        if(info.getClassname() != null && !info.getClassname().equals("")){
            View layout = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
            TextView textView = layout.findViewById(R.id.tv_item_meta_string);
            textView.setText(info.getClassname());
            mMetaLayout.addView(layout);
        }
        if(info.getWeight() != null && !info.getWeight().equals("")){
            View layout = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
            TextView textView = layout.findViewById(R.id.tv_item_meta_string);
            textView.setText(info.getWeight() + "吨");
            mMetaLayout.addView(layout);
        }
        if(info.getTruck_type() != null && !info.getTruck_type().equals("")){
            String truckType = info.getTruck_type();
            String[] truckTypeArray = truckType.split(",");

            int loopSize = truckTypeArray.length > 2? 2 : truckTypeArray.length;
            for(int index = 0; index < loopSize; index++){
                View layout = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(truckTypeArray[index]);
                if(index == 1 && truckTypeArray.length > 2){
                    LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(0, 0, 0, 0);
                    textView.setLayoutParams(layoutParams);
                }
                mMetaLayout.addView(layout);
                if(index == 1 && truckTypeArray.length > 2){
                    View layoutMore = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
                    TextView textViewMore = layoutMore.findViewById(R.id.tv_item_meta_string);
                    textViewMore.setBackground(null);
                    textViewMore.setText("……");
                    mMetaLayout.addView(layoutMore);
                }
            }
            /*if(truckTypeArray.length > 2){
                View layout = LayoutInflater.from(context).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setBackground(null);
                textView.setText("……");
                mMetaLayout.addView(layout);
            }*/
        }
        Glide.with(context).load(info.getIcon()).into(mLevelView);

        if(info.getLoadtime2() != null){
            mLoadTime.setText(info.getLoadtime() + "~" + info.getLoadtime2());
        }
        else {
            mLoadTime.setText(info.getLoadtime());
        }
        mExpectedPrice.setText(info.getExpected_price());
        if(info.getPay_type().equals("2")){
            mPayOnline.setVisibility(VISIBLE);
        }
        else{
            mPayOnline.setVisibility(GONE);
        }
        if(UserInfoController.Instance().getUserInfo() != null &&
            UserInfoController.Instance().getUserInfo().getRole() != 1){
            mShowPrice.setVisibility(VISIBLE);
        }
        else{
            mShowPrice.setVisibility(GONE);
        }

        mLayout = itemLayout.findViewById(R.id.layout_Resource_info_item);
        mLayout.setOnClickListener(this);
    }

    public void bindData(ResourceInfo info){
        if(info != null){
            mFromName.setText(info.getFrom_name());
            mToName.setText(info.getTo_name());
            mSourceDistance.setText(info.getSource_distance() + "km");
            mRegTime.setText(info.getRegtime() + "发布");
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_grab_order ||
                v.getId() == R.id.layout_Resource_info_item ||
                v.getId() == R.id.sv_resource_item_info){
            Intent intent = new Intent(mContext, ResourceDetailActivity.class);
            intent.putExtra("recid", mResourceInfo.getRecid());
            mContext.startActivity(intent);
        }
    }
}
